package com.personal.cuncunxing.module.home.recommend;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.personal.cuncunxing.R;
import com.personal.cuncunxing.common.OkHttpUtils;
import com.personal.cuncunxing.common.ResultCallback;
import com.personal.cuncunxing.common.StaticFiled;
import com.personal.cuncunxing.module.home.pojo.Movie;
import com.personal.cuncunxing.module.player.PlayerActivity;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Request;


/**
 * Description:
 * Created by kode on 2020/5/26.
 */
public class RecommendSubRVAdapter extends RecyclerView.Adapter<RecommendSubRVAdapter.ViewHolder> {

    private static final String TAG = "RecommendSubRVAdapter";
    private OnItemClick onItemClick;
    private Context context;
    private List<Movie> movieList;
    private List<String> playUrlList;

    RecommendSubRVAdapter(Context context, List<Movie> movieList) {
        this.context = context;
        this.movieList = movieList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recommend_sub_item, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        Log.d(TAG, "onBindViewHolder: " + position);
        if (movieList != null) {
            holder.recommend_title_tv.setText(movieList.get(position).getTitle());
            loadImg(position, holder);
        }
        holder.recommend_card_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //onItemClick.OnItemClick(movieList.get(position).getPlayUrl());
                Intent intent = new Intent(context, PlayerActivity.class);
                intent.putExtra("playUrl",movieList.get(position).getPlayUrl().get(0));
                context.getApplicationContext().startActivity(intent);
                Toast.makeText(context, "您点击了" + position, Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 加载图片
     *
     * @param position
     * @param holder
     */
    private void loadImg(final int position, final ViewHolder holder) {
        //设置图片圆角角度
        RoundedCorners roundedCorners = new RoundedCorners(16);
        //通过RequestOptions扩展功能
        final RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
        OkHttpUtils.getInstance(context).getAsyncHttp(StaticFiled.JSOUP_HOST + movieList.get(position).getUrl(),
                new ResultCallback() {
                    @Override
                    public void onError(Request request, Exception e) {

                    }

                    @Override
                    public void onResponse(String str) throws IOException {
                        Document document = Jsoup.parse(str);
                        Elements elements = document.select("div[class=detail-pic fn-left]");
                        for (Element element : elements) {
                            //Log.d(TAG, "onResponse: " + element.select("img").attr("src"));
                            Glide.with(context)
                                    .asBitmap()
                                    .apply(options)
                                    .load(element.select("img").attr("src"))
                                    .into(holder.movies_iv);
                        }
                        //爬取电影播放资源
                        Elements element = Jsoup.parse(str).select("div[class=ui-box download-focus]")
                                .select("ul[class=download-group fn-clear downurl]")
                                .select("li");
                        List<String> stringList = new ArrayList<>();
                        for (Element element1:element){
                            stringList.add(element1.select("a").attr("href"));
                            Log.d(TAG, "onResponse: \n" + element1.select("a").attr("href"));
                        }
                        movieList.get(position).setPlayUrl(stringList);
                    }
                });
    }

    @Override
    public int getItemCount() {
        return 6;
    }

    public void setOnSubItemClick(OnItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout recommend_card_ll;
        TextView recommend_title_tv;
        ImageView movies_iv;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            recommend_card_ll = itemView.findViewById(R.id.recommend_card_ll);
            recommend_title_tv = itemView.findViewById(R.id.recommend_title_tv);
            movies_iv = itemView.findViewById(R.id.movies_iv);
        }
    }

    interface OnItemClick {
        void OnItemClick(List<String> playUrl);
    }
}
